﻿#include "StatusServiceImpl.h"

#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

#include "ConfigMgr.h"
#include "message.pb.h"

std::string generate_unique_string() {
  // 创建UUID对象
  boost::uuids::uuid uuid = boost::uuids::random_generator()();

  // 将UUID转换为字符串
  std::string unique_string = to_string(uuid);

  return unique_string;
}

StatusServiceImpl::StatusServiceImpl() {
  auto& cfg = ConfigMgr::GetInstance();

  ChatServer server;
  server.host = cfg["chatserver1"]["Host"];
  server.port = cfg["chatserver1"]["Port"];
  _servers.push_back(server);

  server.host = cfg["chatserver2"]["Host"];
  server.port = cfg["chatserver2"]["Port"];
  _servers.push_back(server);
}

Status StatusServiceImpl::GetChartServer(ServerContext* context,
                                         const GetChatServerReq* requeset,
                                         GetChatServerRsp* response) {
  _serversIndex = (_serversIndex++) % (_servers.size());
  auto& server = _servers[_serversIndex];
  response->set_host(server.host);
  response->set_port(server.port);
  response->set_error(ErrorCodes::Success);
  response->set_token(generate_unique_string());

  return Status::OK;
}
